package com.wsdcd.zlg.sb.controller;

import com.wsdcd.zlg.sb.core.constant.Constant;
import com.wsdcd.zlg.sb.core.exception.ERPException;
import com.wsdcd.zlg.sb.model.entity.User;
import com.wsdcd.zlg.sb.model.service.UserService;
import com.wsdcd.zlg.sb.util.AjaxObject;
import com.wsdcd.zlg.sb.util.PageParameter;
import com.wsdcd.zlg.sb.util.PagerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@AllArgsConstructor
@Controller
@RequestMapping("/user")
public class UserController {
	private final UserService userService;
	private final HttpServletRequest request;
	private final HttpServletResponse response;
	
	@RequestMapping("login")
	@ResponseBody
	public AjaxObject login(User user, Model model)
			throws IOException {
		try {
			User loginUser = userService.login(user);
			request.getSession().setAttribute(Constant.LOGIN_USER, loginUser);
			return new AjaxObject(1, "登录成功");
		} catch (ERPException e) {
			String msg = e.getMessage();
			return new AjaxObject(0, msg);
		}
	}

	@RequestMapping("index")
	public String index()
			throws IOException {
		return "index";
	}
	
	@RequestMapping("logout")
	public String logout() {
		request.getSession().invalidate();
		return "redirect:/";
	}
	
	@RequestMapping("list")
	public String userList() {
		return "user/list";
	}

	@ResponseBody
	@RequestMapping("lists")
	public PagerUtil<User> userLists(int page,int limit,String username) {
		PagerUtil<User> pager;
		PageParameter pageParameter;
		if(username == null || username.trim().equals("")){
			pageParameter = new PageParameter(page, limit);
		}else{
			pageParameter = new PageParameter(page, limit, "username", username.trim());
		}
		pager = userService.selectByPage(pageParameter);
		return pager;
	}
	
	@RequestMapping("add")
	public String userAdd() {
		return "user/add";
	}

	@ResponseBody
	@RequestMapping("adds")
	public AjaxObject userAdds(String username,String password,String passwordTrue) {
		if(password == null || passwordTrue == null){
			String msg = "输入的密码为空";
			return new AjaxObject(0, msg);
		}else if(!password.equals(passwordTrue)){
			String msg = "两次输入的密码不相同";
			return new AjaxObject(0, msg);
		}
		
		String nickname = request.getParameter("nickname");
		String sexString = request.getParameter("sex");
		boolean sex = false;
		if(sexString == null){
			String msg = "性别不能不选";
			return new AjaxObject(0, msg);
		}else if(sexString.equals("男")){
			sex = true;
		}
		int age = Integer.parseInt(request.getParameter("age"));
		User user = new User(username, password, nickname, sex, age);
		try {
			userService.insert(user);
			String msg = "添加成功";
			return new AjaxObject(1, msg);
		} catch (ERPException e) {
			String msg = e.getMessage();
			return new AjaxObject(0, msg);
		}
	}
	
	@RequestMapping("info")
	public String userInfo() {
		return "user/info";
	}

	@ResponseBody
	@RequestMapping("updates")
	public AjaxObject userUpdate() {
		int id = Integer.parseInt(request.getParameter("id"));
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		String nickname = request.getParameter("nickname");
		String sexString = request.getParameter("sex");
		boolean sex = false;
		if(sexString == null){
			String msg = "性别不能不选";
			return new AjaxObject(0, msg);
		}else if(sexString.equals("男")){
			sex = true;
		}
		int age = Integer.parseInt(request.getParameter("age"));
		User user = new User(id,username, password, nickname, sex, age);
		try {
			userService.update(user);
			String msg = "修改成功";
			return new AjaxObject(1, msg);
		} catch (ERPException e) {
			String msg = e.getMessage();
			return new AjaxObject(0, msg);
		}
	}
	
	@RequestMapping("delete")
	public String userDelete() {
		return "user/delete";
	}

	@ResponseBody
	@RequestMapping("deletes")
	public AjaxObject userDeletes ()
			throws IOException{
		int id = Integer.parseInt(request.getParameter("id"));
		HttpSession session = request.getSession();
		User loginUser = (User) session.getAttribute("loginUser");
		int loginId = loginUser.getId();
		if(loginId == id){
			String msg = "删除失败";
			return new AjaxObject(0, msg);
		}else{
			userService.delete(id);
			String msg = "删除成功";
			return new AjaxObject(1, msg);
		}
	}
}
